package com.stu.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

import com.stu.model.Student;

public class StudentDao extends BaseDao {
	
	/**
	 * 学生信息添加
	 */
	public boolean add(Student student) {
		String sql = "insert into s_stu (id,name,score) values (?,?,?)";
		try {
			PreparedStatement preparedStatement = con.prepareStatement(sql);
			int num = (int)(Math.random()*100+1);
			preparedStatement.setString(1, Integer.toString(num));
			preparedStatement.setString(2, student.getName());
			preparedStatement.setString(3, student.getScore());
			if (preparedStatement.executeUpdate() > 0) {return true;}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		return false;
	}
	/**
	 * 学生信息删除
	 */
	public boolean del(Student student) {
		String sql = "delete from s_stu where id=? and name=?";
		try {
			PreparedStatement preparedStatement = con.prepareStatement(sql);
			preparedStatement.setString(1, student.getId());
			preparedStatement.setString(2, student.getName());
			if (preparedStatement.executeUpdate() > 0) {return true;}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	/**
	 * 学生信息更新
	 */
	public boolean update(Student student) {
		String sql = "update s_stu set score=? where id=? and name=?";
		try {
			PreparedStatement preparedStatement = con.prepareStatement(sql);
			preparedStatement.setString(1, student.getScore());
			preparedStatement.setString(2, student.getId());
			preparedStatement.setString(3, student.getName());
			if (preparedStatement.executeUpdate() > 0) {return true;}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * 查询
	 * 返回一个结果string
	 */
	public ResultSet query() {
		String sql = "select * from s_stu";
		ResultSet rs = null;
		Statement stmt;
		try {
			stmt = con.createStatement();
			rs=stmt.executeQuery(sql);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return rs;	
	}
}

